using UnityEngine;
using System.Collections;

public class PlayScene : MonoBehaviour, CustomScene
{
	public MUIManager uimgr;
	
	public SlotMachine playerSlot;
	public SlotMachine monsterSlot;
	
	public MText playerlv_txt;
	public MProgress playerhp_bar;
	public MProgress playerxp_bar;
	public MText playerhp_txt;
	public MText playerxp_txt;
	
	public MText playeratk_txt;
	public MText playerdef_txt;
	public MText playerluk_txt;
	
	
	public MProgress monhp_bar;
	public MText monhp_txt;
	
	public MButton []Btn;
	public MScrollView []sView;
	 
    void Start()
    {
		uimgr.scene = this;
        Camera.main.orthographicSize = 319 / 2;
        Vector3 temp = Camera.main.transform.position;
        Camera.main.transform.position = new Vector3(temp.x, temp.y, -320 / 2);
		
		InitPlayer();
		InitMonster();
    }
	
	void InitPlayer()
	{
		string path = "Slot/";
		string [] str = { "atk", "def", "etc" };
		
		MControl item = null;
		for(int j=0; j<3; ++j)
		{
			for(int i=0; i<4; ++i)
			{
				Texture t = Resources.Load(path + "icon_" + str[j] + "_0" + (i%3+1) + "_off", typeof(Texture2D)) as Texture2D;
				Texture t2 = Resources.Load(path + "icon_" + str[j] + "_0" + (i%3+1) + "_on", typeof(Texture2D)) as Texture2D;
				int d = t.height;
				int d2 = t2.height;
				item = sView[j].AddItem(0, t.height * i + sView[j].magin * i , 100 + i + 1, t, t2);
			}
		}
		CreateSlot(path, str, playerSlot, new Vector2(154, 236));
		playerSlot.SetScene(this);
	}
	
	void InitMonster()
	{
		string path = "Slot/";
		string [] str = { "atk", "def", "etc" };
		
		CreateSlot(path, str, monsterSlot, new Vector2(348, 26));
		monsterSlot.SetScene(this);
	}
	
	void CreateSlot(string path, string [] str, SlotMachine sm, Vector2 v)
	{
		sm.Create (v.x, v.y, 45);
		for(int j=0; j<3; ++j)
		{
			for(int i=0; i<4; ++i)
			{
				sm.AddCard(j, 0, -32*i, 10, (Texture)Resources.Load(path + "icon_" + str[j] + "_0" + (i%3+1) + "_off"));
			}
		}
		//playerSlot.SlotStart();
		//playerSlot.SlotStop();
	}
	
	void PlayerSetting()
	{
		playerlv_txt.Text = "" + CharacterInfo.Instance().m_Level;
		
		playerhp_bar.MaxValue = CharacterInfo.Instance().m_MaxHp;
		playerhp_bar.Value = CharacterInfo.Instance().m_Hp;
		
		playerxp_bar.MaxValue = CharacterInfo.Instance().m_MaxXp;
		playerxp_bar.Value = CharacterInfo.Instance().m_Xp;
		
		playerhp_txt.Text = string.Format("Hp {0}/{1}", playerhp_bar.Value, playerhp_bar.MaxValue);
		playerxp_txt.Text = string.Format("Xp {0}/{1}", playerxp_bar.Value, playerxp_bar.MaxValue);
		
		playeratk_txt.Text = "ATK " + CharacterInfo.Instance().m_Atk;
		playerdef_txt.Text = "DEF " + CharacterInfo.Instance().m_Def;
		playerluk_txt.Text = "LUK " + CharacterInfo.Instance().m_Luk;
		
	}
	
	void MonsterSetting()
	{
		monhp_bar.MaxValue = MonsterInfo.Instance().m_MaxHp;
		monhp_bar.value = MonsterInfo.Instance().m_Hp;
		
		monhp_txt.Text = string.Format("Hp {0}/{1}", monhp_bar.value, monhp_bar.MaxValue);
	}

	void Update () 
	{
		PlayerSetting();
		MonsterSetting();
	}
	
	void OnGUI()
	{
	}	

	void DoWindow (int windowID) 
	{
	}
	
	public void OnSlotEvent(Slot [] s, SlotMachine sm)
	{
		if( sm == playerSlot )
		{
			float t1 = MonsterInfo.Instance().m_Def - CharacterInfo.Instance().m_Atk;
			if( t1 >= 0 )
				t1 = -1;
			float t2 = MonsterInfo.Instance().m_Hp + t1;
			if( t2 < 0 )
			{
				t2 = 0;
			}
			
			MonsterInfo.Instance().m_Hp = t2;
			monsterSlot.SlotStart();
			monsterSlot.SlotStop();
		}
		else
		{
			float t1 = CharacterInfo.Instance().m_Def - MonsterInfo.Instance().m_Atk;
			if( t1 >= 0 )
				t1 = -1;
			float t2 = CharacterInfo.Instance().m_Hp + t1;
			if( t2 < 0 )
			{
				t2 = 0;
			}
			
			CharacterInfo.Instance().m_Hp = t2;
		}
	}

    public virtual void OnUIEvent(int number, object obj)
    {
		IconBtn(number, obj);
		ScollViewItem(number, obj);
		
		//	2000 번대 슬롯 머신
		if( number == 2000 )	//	Spin, Stop버튼
		{
			MHoverButton btn = obj as MHoverButton;
			if(!btn.IsDown && playerSlot.state == 3)
			{
				playerxp_txt.Text = "" + number + " is click1";
				playerSlot.SlotStart();
			}
			
			else if( btn.IsDown && playerSlot.state == 0)
			{
				playerxp_txt.Text = "" + number + " is click2";
				playerSlot.SlotStop();
			}
			
			else 
			{
				playerxp_txt.Text = "" + number + " is click3";
				btn.IsDown = !btn.IsDown;
			}
			//playerxp_txt.Text = "" + number + " is click4";
		}
		
		if( number == 1000 )
		{
			Application.LoadLevel("MainScene");
		}
    }
	
	public void IconBtn(int number, object obj)
	{
		if( number > 3 )
			return;
		
		sView[number-1].SetEnable(!sView[number-1].GetEnable());
	}
	
	public void ScollViewItem(int id, object obj)
	{
		
		switch(id)
		{
		case 101:
		case 102:
		case 103:
		case 201:
		case 202:
		case 203:
		case 301:
		case 302:
		case 303:
			break;
		default:
			return;
		}
		/*
		for(int i=0; i<3; ++i)
		{
			int _id = sView[i].Id;
			if( _id < id || _id + sView[i].GetItemCount() > id )
			{
				return;
			}
		}
		*/
		Application.LoadLevel("MainScene");
		
		MScrollViewItem item = obj as MScrollViewItem;
		Btn[id/100-1].view = item.view;
		Btn[id/100-1].view2 = item.view2;
		//sView[id/100-1]. = !sView[id/100-1].Enable;
	}
}
